//
//  Context.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/29.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "Context.h"

@interface Context()

@end

@implementation Context

+ (instancetype)sharedInstance {
    static Context *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (instancetype)init {
    self = [super init];
    if(self) {
        [self initContext];
    }
    return self;
}

- (void)initContext {
    _selectPackageId = nil;
}

- (BOOL)isLogin {
    //TODO -- 判断用户是否已经登录
    if(!self.loginID) {
        return NO;
    }
    return YES;
}

#pragma mark - setter and getter
- (void)setLoginID:(NSString *)loginID {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if(loginID != nil) {
        [userDefaults setObject:loginID forKey:@"loginID"];
    } else {
        [userDefaults removeObjectForKey:@"loginID"];
    }
}

- (NSString *)loginID {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *loginID = [userDefaults objectForKey:@"loginID"];
    return loginID;
}

- (void)setMobile:(NSString *)mobile {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if(mobile != nil) {
        [userDefaults setObject:mobile forKey:@"mobile"];
    } else {
        [userDefaults removeObjectForKey:@"mobile"];
    }
}

- (NSString *)mobile {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *mobile = [userDefaults objectForKey:@"mobile"];
    return mobile;
}

- (void)setAccountId:(NSString *)accountId {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if(accountId != nil) {
        [userDefaults setObject:accountId forKey:@"accountId"];
    } else {
        [userDefaults removeObjectForKey:@"accountId"];
    }
}

- (NSString *)accountId {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *accountId = [userDefaults objectForKey:@"accountId"];
    return accountId;
}

- (NSArray *)addressArray {
    NSString *path = (NSString *)[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fileName = [path stringByAppendingPathComponent:@"address.plist"];
    NSArray *tempArray = [NSArray arrayWithContentsOfFile:fileName];
    return tempArray;
}

- (NSArray *)bankArray {
    NSString *path = (NSString *)[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fileName = [path stringByAppendingPathComponent:@"bank.plist"];
    NSArray *tempArray = [NSArray arrayWithContentsOfFile:fileName];
    return tempArray;
}

- (NSArray *)commodityTypeArray {
    NSString *path = (NSString *)[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fileName = [path stringByAppendingPathComponent:@"commodity.plist"];
    NSArray *tempArray = [NSArray arrayWithContentsOfFile:fileName];
    return tempArray;
}

- (NSArray *)providerArray {
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:0];
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithCapacity:0];
    [dict1 setObject:@"0" forKey:@"providerId"];
    [dict1 setObject:@"中国移动" forKey:@"provider"];
    [tempArray addObject:dict1];
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:0];
    [dict2 setObject:@"1" forKey:@"providerId"];
    [dict2 setObject:@"中国联通" forKey:@"provider"];
    [tempArray addObject:dict2];
    NSMutableDictionary *dict3 = [NSMutableDictionary dictionaryWithCapacity:0];
    [dict3 setObject:@"2" forKey:@"providerId"];
    [dict3 setObject:@"中国电信" forKey:@"provider"];
    [tempArray addObject:dict3];
    NSMutableDictionary *dict4 = [NSMutableDictionary dictionaryWithCapacity:0];
    [dict4 setObject:@"3" forKey:@"providerId"];
    [dict4 setObject:@"其他" forKey:@"provider"];
    [tempArray addObject:dict4];
    return tempArray;
}

- (void)setAccountInfo:(AccountInfo *)accountInfo {
    _accountInfo = accountInfo;
    if(_accountInfo) {
        [self setAccountId:_accountInfo.userId];
    } else {
        [self setAccountId:nil];
    }
}

@end
